package durst.action;

import java.sql.SQLException;
import java.util.List;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.validation.EmailTypeConverter;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrors;
import net.sourceforge.stripes.validation.ValidationMethod;

import com.j256.ormlite.dao.Dao;

import durst.action.account.CalendarActionBean;
import durst.data.Account;
import durst.data.UserCalendar;
import durst.data.UserCalendar.SharedStatus;
import durst.data.dao.DaoFactory;
import durst.data.dao.impl.AccountDao;

public class CreateAccountActionBean extends CalendarBasicAction {
	private static final String DEFAULT_NAME = "Default Calendar";

	@ValidateNestedProperties({
			@Validate(field = "username", required = true, on = "create", mask="\\w+", minlength = 6, maxlength = 20),
			@Validate(field = "password", required = true, on = "create", minlength = 6, maxlength = 32),
			@Validate(field = "email", required = true, on = "create", converter = EmailTypeConverter.class, maxlength=32),
			@Validate(field = "firstName", required = true, on = "create", mask = "\\w+", maxlength = 15),
			@Validate(field = "lastName", required = true, on = "create", mask = "\\w+", maxlength = 15) })
	private Account account;

	@Validate(required = true, on = "create", expression = "this == account.password")
	private String confirm;
	
	@SuppressWarnings("unchecked")
	@ValidationMethod(on = "create")
	public void validateUser(ValidationErrors errors) throws SQLException {
		List<Account> res = ((AccountDao<Account, Integer>) DaoFactory
				.getInstance().createDao(Account.class))
				.queryByUsernameOrEmail(account.getUsername(),
						account.getEmail());

		for (Account acc : res) {
			if (acc.getUsername().equals(account.getUsername()))
				errors.add("username", new LocalizableError(
						"durst.action.createaccount.username.alreadyused"));
			else if (acc.getEmail().equals(account.getEmail()))
				errors.add("email", new LocalizableError(
						"durst.action.createaccount.email.alreadyused"));
		}

		if (errors.size() > 0)
			context.setAccount(null);
	}

	@DefaultHandler
	public Resolution create() {
		try {
			Dao<Account, Integer> accDao = DaoFactory.getInstance().createDao(
					Account.class);
			account.changePassword(account.getPassword());
			accDao.create(account);

			UserCalendar cal = new UserCalendar(DEFAULT_NAME,
					SharedStatus.PRIVATE, account, true, true);
			DaoFactory.getInstance().createDao(UserCalendar.class).create(cal);

			accDao.refresh(account);
			((CalendarContext) context).setAccount(account);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return new RedirectResolution(CalendarActionBean.class);
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public Account getAccount() {
		return account;
	}

	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}

	public String getConfirm() {
		return confirm;
	}
}